<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QMutexLocker Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QMutexLocker Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QMutexLocker class is a convenience class that simplifies
locking and unlocking mutexes. <a href="#details">More...</a></p>

<h3>Methods</h3><ul><li><div class="fn" /><b><a href="qmutexlocker.html#QMutexLocker">__init__</a></b> (<i>self</i>, QMutex&#160;<i>m</i>)</li><li><div class="fn" />QMutex <b><a href="qmutexlocker.html#mutex">mutex</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qmutexlocker.html#relock">relock</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qmutexlocker.html#unlock">unlock</a></b> (<i>self</i>)</li></ul><h3>Special Methods</h3><ul><li><div class="fn" />object <b><a href="qmutexlocker.html#__enter__">__enter__</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qmutexlocker.html#__exit__">__exit__</a></b> (<i>self</i>, object&#160;<i>type</i>, object&#160;<i>value</i>, object&#160;<i>traceback</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QMutexLocker class is a convenience class that simplifies
locking and unlocking mutexes.</p>
<p>Locking and unlocking a <a href="qmutex.html">QMutex</a> in
complex functions and statements or in exception handling code is
error-prone and difficult to debug. QMutexLocker can be used in
such situations to ensure that the state of the mutex is always
well-defined.</p>
<p>QMutexLocker should be created within a function where a
<a href="qmutex.html">QMutex</a> needs to be locked. The mutex is
locked when QMutexLocker is created. You can unlock and relock the
mutex with <tt>unlock()</tt> and <tt>relock()</tt>. If locked, the
mutex will be unlocked when the QMutexLocker is destroyed.</p>
<p>For example, this complex function locks a <a href="qmutex.html">QMutex</a> upon entering the function and unlocks the
mutex at all the exit points:</p>
<pre class="cpp">
 <span class="type">int</span> complexFunction(<span class="type">int</span> flag)
 {
     mutex<span class="operator">.</span>lock();

     <span class="type">int</span> retVal <span class="operator">=</span> <span class="number">0</span>;

     <span class="keyword">switch</span> (flag) {
     <span class="keyword">case</span> <span class="number">0</span>:
     <span class="keyword">case</span> <span class="number">1</span>:
         retVal <span class="operator">=</span> moreComplexFunction(flag);
         <span class="keyword">break</span>;
     <span class="keyword">case</span> <span class="number">2</span>:
         {
             <span class="type">int</span> status <span class="operator">=</span> anotherFunction();
             <span class="keyword">if</span> (status <span class="operator">&lt;</span> <span class="number">0</span>) {
                 mutex<span class="operator">.</span>unlock();
                 <span class="keyword">return</span> <span class="operator">-</span><span class="number">2</span>;
             }
             retVal <span class="operator">=</span> status <span class="operator">+</span> flag;
         }
         <span class="keyword">break</span>;
     <span class="keyword">default</span>:
         <span class="keyword">if</span> (flag <span class="operator">&gt;</span> <span class="number">10</span>) {
             mutex<span class="operator">.</span>unlock();
             <span class="keyword">return</span> <span class="operator">-</span><span class="number">1</span>;
         }
         <span class="keyword">break</span>;
     }

     mutex<span class="operator">.</span>unlock();
     <span class="keyword">return</span> retVal;
 }
</pre>
<p>This example function will get more complicated as it is
developed, which increases the likelihood that errors will
occur.</p>
<p>Using QMutexLocker greatly simplifies the code, and makes it
more readable:</p>
<pre class="cpp">
 <span class="type">int</span> complexFunction(<span class="type">int</span> flag)
 {
     <span class="type">QMutexLocker</span> locker(<span class="operator">&amp;</span>mutex);

     <span class="type">int</span> retVal <span class="operator">=</span> <span class="number">0</span>;

     <span class="keyword">switch</span> (flag) {
     <span class="keyword">case</span> <span class="number">0</span>:
     <span class="keyword">case</span> <span class="number">1</span>:
         <span class="keyword">return</span> moreComplexFunction(flag);
     <span class="keyword">case</span> <span class="number">2</span>:
         {
             <span class="type">int</span> status <span class="operator">=</span> anotherFunction();
             <span class="keyword">if</span> (status <span class="operator">&lt;</span> <span class="number">0</span>)
                 <span class="keyword">return</span> <span class="operator">-</span><span class="number">2</span>;
             retVal <span class="operator">=</span> status <span class="operator">+</span> flag;
         }
         <span class="keyword">break</span>;
     <span class="keyword">default</span>:
         <span class="keyword">if</span> (flag <span class="operator">&gt;</span> <span class="number">10</span>)
             <span class="keyword">return</span> <span class="operator">-</span><span class="number">1</span>;
         <span class="keyword">break</span>;
     }

     <span class="keyword">return</span> retVal;
 }
</pre>
<p>Now, the mutex will always be unlocked when the QMutexLocker
object is destroyed (when the function returns since
<tt>locker</tt> is an auto variable).</p>
<p>The same principle applies to code that throws and catches
exceptions. An exception that is not caught in the function that
has locked the mutex has no way of unlocking the mutex before the
exception is passed up the stack to the calling function.</p>
<p>QMutexLocker also provides a <tt>mutex()</tt> member function
that returns the mutex on which the QMutexLocker is operating. This
is useful for code that needs access to the mutex, such as <a href="qwaitcondition.html#wait">QWaitCondition.wait</a>(). For
example:</p>
<pre class="cpp">
 <span class="keyword">class</span> SignalWaiter
 {
 <span class="keyword">private</span>:
     <span class="type">QMutexLocker</span> locker;

 <span class="keyword">public</span>:
     SignalWaiter(<span class="type"><a href="qmutex.html">QMutex</a></span> <span class="operator">*</span>mutex)
         : locker(mutex)
     {
     }

     <span class="type">void</span> waitForSignal()
     {
         <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
         <span class="keyword">while</span> (<span class="operator">!</span>signalled)
             waitCondition<span class="operator">.</span>wait(locker<span class="operator">.</span>mutex());
         <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
     }
 };
</pre><hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QMutexLocker" />QMutexLocker.__init__ (<i>self</i>, <a href="qmutex.html">QMutex</a>&#160;<i>m</i>)</h3><p>Constructs a <a href="qmutexlocker.html">QMutexLocker</a> and
locks <i>mutex</i>. The mutex will be unlocked when the <a href="qmutexlocker.html">QMutexLocker</a> is destroyed. If <i>mutex</i>
is zero, <a href="qmutexlocker.html">QMutexLocker</a> does
nothing.</p>
<p><b>See also</b> <a href="qmutex.html#lock">QMutex.lock</a>().</p>


<h3 class="fn"><a name="mutex" /><a href="qmutex.html">QMutex</a> QMutexLocker.mutex (<i>self</i>)</h3><p>Returns a pointer to the mutex that was locked in the
constructor.</p>


<h3 class="fn"><a name="relock" />QMutexLocker.relock (<i>self</i>)</h3><p>Relocks an unlocked mutex locker.</p>
<p><b>See also</b> <a href="qmutexlocker.html#unlock">unlock</a>().</p>


<h3 class="fn"><a name="unlock" />QMutexLocker.unlock (<i>self</i>)</h3><p>Unlocks this mutex locker. You can use <tt>relock()</tt> to lock
it again. It does not need to be locked when destroyed.</p>
<p><b>See also</b> <a href="qmutexlocker.html#relock">relock</a>().</p>
<h3 class="fn"><a name="__enter__" />object QMutexLocker.__enter__ (<i>self</i>)</h3><h3 class="fn"><a name="__exit__" />QMutexLocker.__exit__ (<i>self</i>, object&#160;<i>type</i>, object&#160;<i>value</i>, object&#160;<i>traceback</i>)</h3><address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;snapshot-4.9.5-9eb6aac99275 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt&#160;4.8.3</td></tr></table></div></address></body></html>